package svc

import (
	"database/sql"

	"mall/common/modelx"

	"mall/service/attachment/repository"
	"mall/service/attachment/rpc/internal/config"

	"github.com/zeromicro/go-queue/kq"
	"github.com/zeromicro/go-zero/core/logx"
)

type ServiceContext struct {
	Config config.Config

	Attachment repository.AttachmentRepository

	DB             *sql.DB
	MediaTerm      repository.MediaTermRepository
	MediaFile      repository.MediaFileRepository
	FileInfo       repository.FileInfoRepository
	FileLog        repository.FileLogRepository
	CurrentFileLog repository.CurrentFileLogRepository

	KqAttachmentSyncQiniuClient *kq.Pusher
}

func NewServiceContext(c config.Config) *ServiceContext {
	db, err := modelx.NewMysqlConn(c.Mysql.DataSource)
	if err != nil {
		logx.Must(err)
	}

	return &ServiceContext{
		Config: c,

		Attachment: repository.NewAttachmentRepository(c.Mysql.DataSource, c.CacheRedis),

		DB:             db,
		MediaTerm:      repository.NewMediaTermRepository(db, c.CacheRedis),
		MediaFile:      repository.NewMediaFileRepository(db, c.CacheRedis),
		FileInfo:       repository.NewFileInfoRepository(db),
		FileLog:        repository.NewFileLogRepository(db),
		CurrentFileLog: repository.NewCurrentFileLogRepository(db, c.CacheRedis),

		KqAttachmentSyncQiniuClient: kq.NewPusher(c.KqAttachmentSyncQiniuConf.Brokers, c.KqAttachmentSyncQiniuConf.Topic),
	}
}
